package com.sky.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.constants.Constants;
import com.sky.domain.CareOrderItem;
import com.sky.domain.OrderCharge;
import com.sky.domain.OrderChargeItem;
import com.sky.dto.OrderChargeDto;
import com.sky.dto.OrderChargeFormDto;
import com.sky.dto.OrderChargeItemDto;
import com.sky.mapper.CareOrderItemMapper;
import com.sky.mapper.OrderChargeItemMapper;
import com.sky.mapper.OrderChargeMapper;
import com.sky.service.OrderChargeService;
import com.sky.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sky
 * @create 2021-07-18 15:00
 */
@Service(methods = {@Method(name = "saveOrderAndItems", retries = 1),
        @Method(name = "paySuccess", retries = 1)})
public class OrderChargeServiceImpl implements OrderChargeService {

    @Autowired
    private OrderChargeMapper orderChargeMapper;

    @Autowired
    private OrderChargeItemMapper orderChargeItemMapper;

    @Autowired
    private CareOrderItemMapper careOrderItemMapper;

    @Override
    public OrderCharge queryOrderChargeByOrderId(String orderId) {
        return this.orderChargeMapper.selectById(orderId);
    }

    @Override
    public DataGridView queryAllOrderChargeForPage(OrderChargeDto orderChargeDto) {
        Page<OrderCharge> page = new Page<>(orderChargeDto.getPageNum(), orderChargeDto.getPageSize());
        QueryWrapper<OrderCharge> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(orderChargeDto.getPatientName()), OrderCharge.COL_PATIENT_NAME, orderChargeDto.getPatientName());
        qw.like(StringUtils.isNotBlank(orderChargeDto.getRegId()), OrderCharge.COL_REG_ID, orderChargeDto.getRegId());
        qw.orderByDesc(OrderCharge.COL_CREATE_TIME);
        this.orderChargeMapper.selectPage(page, qw);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    @Override
    public List<OrderChargeItem> queryOrderChargeItemByOrderId(String orderId) {
        QueryWrapper<OrderChargeItem> qw = new QueryWrapper<>();
        qw.eq(null != orderId, OrderChargeItem.COL_ORDER_ID, orderId);
        return this.orderChargeItemMapper.selectList(qw);
    }

    @Override
    public void saveOrderAndItems(OrderChargeFormDto orderChargeFormDto) {
        // 获取订单
        OrderChargeDto orderChargeDto = orderChargeFormDto.getOrderChargeDto();
        // 获取订单详情
        List<OrderChargeItemDto> orderChargeItemDtos = orderChargeFormDto.getOrderChargeItemDtos();

        // 添加订单
        OrderCharge orderCharge = new OrderCharge();
        BeanUtil.copyProperties(orderChargeDto, orderCharge);
        orderCharge.setOrderStatus(Constants.ORDER_STATUS_0);
        orderCharge.setCreateTime(DateUtil.date());
        orderCharge.setCreateBy(orderChargeFormDto.getSimpleUser().getUserName());
        this.orderChargeMapper.insert(orderCharge);

        // 添加订单详情
        for (OrderChargeItemDto orderChargeItemDto : orderChargeItemDtos) {
            OrderChargeItem orderChargeItem = new OrderChargeItem();
            BeanUtils.copyProperties(orderChargeItemDto, orderCharge);
            orderCharge.setOrderId(orderCharge.getOrderId());
            orderCharge.setOrderStatus(Constants.ORDER_DETAILS_STATUS_0);
            this.orderChargeItemMapper.insert(orderChargeItem);
        }
    }

    @Override
    public void paySuccess(String orderId, String payPlatformId, String payType) {
        // 获取订单
        OrderCharge orderCharge = this.orderChargeMapper.selectById(orderId);
        // 设置平台交易编号
        orderCharge.setPayPlatformId(payPlatformId);
        // 设置支付类型
        orderCharge.setPayType(payType);
        // 设置支付时间
        orderCharge.setPayTime(DateUtil.date());
        // 修改订单状态
        orderCharge.setOrderStatus(Constants.ORDER_STATUS_1); //已支付
        // 更新订单状态
        this.orderChargeMapper.updateById(orderCharge);

        // 根据支付订单号查询支付订单详情
        QueryWrapper<OrderChargeItem> qw = new QueryWrapper<>();
        qw.eq(OrderChargeItem.COL_ORDER_ID, orderId);
        List<OrderChargeItem> orderChargeItems = this.orderChargeItemMapper.selectList(qw);
        // 获取各订单详情的Id
        List<String> allItemIds = new ArrayList<>();
        for (OrderChargeItem orderChargeItem : orderChargeItems) {
            allItemIds.add(orderChargeItem.getItemId());
        }

        // 更新收费详情的状态
        OrderChargeItem orderChargeItem = new OrderChargeItem();
        orderChargeItem.setStatus(Constants.ORDER_DETAILS_STATUS_1);
        QueryWrapper<OrderChargeItem> orderItemQw = new QueryWrapper<>();
        orderItemQw.in(OrderChargeItem.COL_ITEM_ID, allItemIds);
        this.orderChargeItemMapper.update(orderChargeItem, orderItemQw);

        // 更新处方详情的状态
        CareOrderItem careItemObj = new CareOrderItem();
        careItemObj.setStatus(Constants.ORDER_DETAILS_STATUS_1);
        QueryWrapper<CareOrderItem> careItemQw = new QueryWrapper<>();
        careItemQw.in(CareOrderItem.COL_ITEM_ID, allItemIds);
        this.careOrderItemMapper.update(careItemObj, careItemQw);
    }

    @Override
    public OrderChargeItem queryOrderChargeItemByItemId(String itemId) {
        return this.orderChargeItemMapper.selectById(itemId);
    }
}
